#!/bin/bash -x
PROJECT_NAME=$1
COPY_DEST_DIR=$2/$PROJECT_NAME
ENV=$3
CURRENT_PWD=$(cd `dirname $0`; pwd)
echo $(pwd)
echo "当前打包环境$3";
echo -e "\e[1;32m提示:${PROJECT_NAME}项目开始构建\n-------------------------------------------------------\e[0m"

#check error
CHECK_ERROR()
{
if [ $? -ne 0 ];then
        echo -e "\e[1;31m[ERROR]: $1\e[0m"
        exit 1
else
        echo -e "\e[1;32m[SUCCESS]: $1\e[0m"
fi
}
cd $CURRENT_PWD

CHECK_ERROR "检查项目依赖包并安装"


cnpm install
if [[ `echo $ENV` != "pro" ]];then
    cnpm run build-test
else
    cnpm run build
fi

cp dist/index.html dist/index.htm

[[ -d $COPY_DEST_DIR ]] || mkdir -p $COPY_DEST_DIR
rsync -aq --exclude *.sh dist/* $COPY_DEST_DIR
CHECK_ERROR "拷贝${CURRENT_PWD}/dist/* 到 ${COPY_DEST_DIR}"
